<?php

require_once ROOT_DIR.'models/TodoList.php';

/**
 * Tries to share a list with a user and echos the result
 * @param int $listID
 * @param int $userID
 */
function shareList($listID, $userID = -1){
	
	if(is_null($userID)) $userID = -1;
	
	$list = TodoList::getList($listID);
	
	if(!is_null($list)){
		if($list->getOwnerID() == $_SESSION['USER_ID'] || $_SESSION['USER_NAME'] == 'admin'){
			$res = TodoList::shareTodoListWithUser($listID, $userID);
			if($res){
				$res = new ResultJSON(false, session_id());
				echo $res->renderView();
			} else {
				$res = new ResultJSON(true, session_id(), ERROR_ACTION_FAILED);
				echo $res->renderView();
			}
		} else {
			$res = new ResultJSON(true, session_id(), ERROR_NOT_ALLOWED);
			echo $res->renderView();
		}
	} else {
		$res = new ResultJSON(true, session_id(), ERROR_LIST_DOES_NOT_EXIST);
		echo $res->renderView();
	}
}

?>